package hzau.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import hzau.constants.ResultConstant;
import hzau.dao.PaperDao;
import hzau.dao.QuestionDao;
import hzau.dao.SelectionQuestionDao;
import hzau.pojo.Paper;
import hzau.pojo.question.Question;
import hzau.pojo.question.SelectionQuestion;
import hzau.pojo.requestParams.QuestionsRegisterParam;
import hzau.pojo.requestParams.SelectionQuestionParam;
import hzau.result.Result;
import hzau.service.QuestionService;
import hzau.utils.ESUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    QuestionDao questionDao;
    @Autowired
    SelectionQuestionDao selectionQuestionDao;
    @Autowired
    PaperDao paperDao;
//    @Autowired
    ESUtil esUtil=new ESUtil();
    @Override
    public Result register(QuestionsRegisterParam question) {

        Paper paper = paperDao.selectById(question.getPaperId());
        if(paper!=null)
        {questionDao.insert(new Question(question));
            return Result.success(null);}
        else return Result.error(ResultConstant.NOT_EXIST,ResultConstant.NO_SUCH_PAPER);

    }

    @Override
    public Result registerSelectedQuestion(SelectionQuestionParam question) {

        Paper paper = paperDao.selectById(question.getPaperId());
        if(paper!=null)
        {selectionQuestionDao.insert(new SelectionQuestion(question));
            return Result.success(null);}
        else return Result.error(ResultConstant.NOT_EXIST,ResultConstant.NO_SUCH_PAPER);

    }

    @Override
    public Result getOneQuestion(Long id) {

        Question question = questionDao.selectById(id);
        return Result.success(question);
    }

    @Override
    public Result getOneSelectionQuestion(Long id) {
        SelectionQuestion question = selectionQuestionDao.selectById(id);
        return Result.success(question);
    }

    @Override
    public Result getAllQuestion(Long id) {
        LambdaQueryWrapper<Question> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Question::getPaperId,id);
        List<Question> questions = questionDao.selectList(lqw);
        return Result.success(questions);
    }

    @Override
    public Result getAllSelectionQuestion(Long id) {
        LambdaQueryWrapper<SelectionQuestion> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SelectionQuestion::getPaperId,id);
        List<SelectionQuestion> questions = selectionQuestionDao.selectList(lqw);
        return Result.success(questions);
    }

    @Override
    public Result updateSelectionQuestion(SelectionQuestion question) {
        LambdaQueryWrapper<SelectionQuestion> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SelectionQuestion::getId,question.getId());
        selectionQuestionDao.updateById(question);
        return Result.success(null);
    }

    @Override
    public Result updateQuestion(Question question) {
        LambdaQueryWrapper<Question> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Question::getId,question.getId());
        questionDao.updateById(question);
        return Result.success(null);
    }

    @Override
    public Result deleteQuestion(Question question) {
        questionDao.deleteById(question.getId());
        return Result.success(null);
    }

    @Override
    public Result deleteSelectionQuestion(SelectionQuestion question) {
        selectionQuestionDao.deleteById(question.getId());
        return Result.success(null);
    }

    @Override
    public Result getOneKindQuestion(Integer type, Long paperId) {
        LambdaQueryWrapper<Question> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Question::getType,type);
        lqw.eq(Question::getPaperId,paperId);
        List<Question> questions = questionDao.selectList(lqw);
        return  Result.success(questions);
    }

    @Override
    public Result registerOrUpdateselecteQuestion(SelectionQuestion question) {
        LambdaQueryWrapper<SelectionQuestion> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SelectionQuestion::getPaperId,question.getPaperId());
        lqw.eq(SelectionQuestion::getQuestionId,question.getQuestionId());
        SelectionQuestion selectionQuestion = selectionQuestionDao.selectOne(lqw);
        if (selectionQuestion!=null){
            selectionQuestionDao.update(question,lqw);
        }else {
            selectionQuestionDao.insert(question);
        }
        return Result.success(null);
    }

    @Override
    public Result registerOrUpdateQuestion(Question question) {
        LambdaQueryWrapper<Question> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Question::getPaperId,question.getPaperId());
        lqw.eq(Question::getQuestionId,question.getQuestionId());
        lqw.eq(Question::getType,question.getType());
        Question question2 = questionDao.selectOne(lqw);
        if (question2!=null){
            questionDao.update(question,lqw);
        }else {
            questionDao.insert(question);
        }
        return Result.success(null);
    }

    @Override
    public Result selectByPaperAndId(Long paperId, Integer id) {
        LambdaQueryWrapper<SelectionQuestion> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SelectionQuestion::getPaperId,paperId);
        lqw.eq(SelectionQuestion::getQuestionId,id);
        SelectionQuestion selectionQuestion = selectionQuestionDao.selectOne(lqw);
        return Result.success(selectionQuestion);
    }

    @Override
    public Result getSelectionNumber(Long paperId) {
        LambdaQueryWrapper<SelectionQuestion> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SelectionQuestion::getPaperId,paperId);
        Integer integer = selectionQuestionDao.selectCount(lambdaQueryWrapper);
        return Result.success(integer);
    }

    @Override
    public Result getQuestionNumber(Long paperId, Short type) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getPaperId,paperId);
        lambdaQueryWrapper.eq(Question::getType,type);
        Integer integer = questionDao.selectCount(lambdaQueryWrapper);
        return Result.success(integer);
    }

    @Override
    public Result selectQuestionByPaperAndId(Long paperId, Short type, Integer id) {
        LambdaQueryWrapper<Question> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Question::getPaperId,paperId);
        lambdaQueryWrapper.eq(Question::getType,type);
        lambdaQueryWrapper.eq(Question::getQuestionId,id);
        Question question = questionDao.selectOne(lambdaQueryWrapper);
        return Result.success(question);
    }

    @Override
    public Result selectQuestionsByPaperAndId(Long paperId, Short type) {
        LambdaQueryWrapper<Question> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Question::getPaperId,paperId);
        lqw.eq(Question::getType,type);
        lqw.orderBy(true,true,Question::getQuestionId);
        List<Question> questions = questionDao.selectList(lqw);
        return Result.success(questions);
    }

    @Override
    public Result getSelectionScore(String description) throws IOException {
        ArrayList<SelectionQuestion> selectionQuestions = esUtil.selectionMatch(description);
        return Result.success(selectionQuestions);
    }
    @Override
    public Result getQuestionScore(String description) throws IOException {
        ArrayList<Question> selectionQuestions = esUtil.QuestionMatch(description);
        return Result.success(selectionQuestions);
    }
}
